<template>
	<view class="home">
		<scroll-view scroll-x class="navScroll">
			<view class="item" :class="index===navIndex ? 'active' : ''" v-for="(item,index) in navArr"
				@click="clickNav(index,item.id)" :key="item.id">{{item.classname}}</view>
		</scroll-view>
		<view class="content">
			<view class="row" v-for="item in newsArr" :key="item.id">
				<newsBox :item="item" @click.native="goDetail(item)"></newsBox>
			</view>
		</view>
		<view class="noData" v-if="!newsArr.length">
			<image src="../../static/images/noData.jpg"></image>
		</view>
		<view class="loading" v-if="newsArr.length">
			<view v-if="loading==1">数据正在加载中...</view>
			<view v-if="loading==2">没有更多数据了...</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				navArr: [],
				newsArr: [],
				currentId: 50,
				currentPage: 1,
				loading: 0
			}
		},
		onLoad() {
			this.getNavData(),
				this.getNewsData()
		},
		onReachBottom() {
			if (this.loading === 2) {
				return
			}
			this.currentPage++,
				this.getNewsData(),
				this.loading = 1
		},
		methods: {
			clickNav(index, id) {
				this.navIndex = index,
					this.currentPage = 1,
					this.newsArr = [],
					this.loading = 0,
					this.currentId = id,
					this.getNewsData()
			},
			goDetail(item) {
				uni.navigateTo({
					url: `/pages/detail/detail?cid=${item.classid}&id=${item.id}`
				})
			},
			getNavData() {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/navlist.php",
					success: res => {
						this.navArr = res.data
					}
				})
			},
			getNewsData() {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/newslist.php",
					data: {
						cid: this.currentId,
						page: this.currentPage
					},
					success: res => {
						console.log(res)
						if (res.data.length === 0) {
							this.loading = 2
						}
						this.newsArr = [...this.newsArr, ...res.data]
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.navScroll {
		height: 100rpx;
		background: #d9d6c3;
		white-space: nowrap;
		position: fixed;
		top: var(--window-top);
		left: 0;
		z-index: 10;

		/deep/ ::-webkit-scrollbar {
			width: 4px !important;
			height: 1px !important;
			overflow: auto !important;
			background: transparent !important;
			-webkit-appearance: auto !important;
			display: block;
		}

		.item {
			display: inline-block;
			font-size: 40rpx;
			line-height: 100rpx;
			padding: 0 20rpx;
			color: "#333";

			&.active {
				color: #33a3dc;
			}
		}
	}

	.content {
		padding: 30rpx;
		padding-top: 130rpx;

		.row {
			padding: 15rpx 0;
			border-bottom: 1px dotted #efefef;
		}
	}

	.noData {
		display: flex;
		justify-content: center;

		image {
			width: 100%;
		}
	}

	.loading {
		text-align: center;
		font-size: 24rpx;
		color: #888;
		padding: 20rpx 0;

		view {
			margin-top: 20rpx;
		}
	}
</style>